home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / nethack.lha / nethack-3.1 / util / dgn_main.c < prev    next >
C/C++ Source or Header  |  1992-10-19  |  3KB  |  152 lines

  1. /*    SCCS Id: @(#)dgn_main.c 3.1    92/03/10    */
  2. /*    Copyright (c) 1989 by Jean-Christophe Collet    */
  3. /*    Copyright (c) 1990 by M. Stephenson        */
  4. /* NetHack may be freely redistributed.  See license for details. */
  5.  
  6. /*
  7.  * This file contains the main function for the parser
  8.  * and some useful functions needed by yacc
  9.  */
  10.  
  11. #include "config.h"
  12.  
  13. #ifdef MICRO
  14. # undef exit
  15. # ifndef AMIGA
  16. extern void FDECL(exit, (int));
  17. # endif
  18. #endif
  19.  
  20. #if defined(MICRO) && !defined(AMIGA)
  21. # define WRMODE "w+b"
  22. #else
  23. # define WRMODE "w+"
  24. #endif
  25.  
  26. #ifdef MAC
  27. # ifdef applec
  28. #  define MPWTOOL
  29. #  include <CursorCtl.h>
  30. # endif
  31. #endif
  32.  
  33. #ifndef MPWTOOL
  34. # define SpinCursor(x)
  35. #endif
  36.  
  37. #define MAX_ERRORS    25
  38.  
  39. extern int line_number;
  40. char *fname = "(stdin)";
  41. int fatal_error = 0;
  42.  
  43. int  FDECL (main, (int, char **));
  44. int  NDECL (yyparse);
  45. void FDECL (yyerror, (char *));
  46. void FDECL (yywarning, (char *));
  47. int  NDECL (yywrap);
  48. void FDECL (init_yyin, (FILE *));
  49. void FDECL (init_yyout, (FILE *));
  50.  
  51. #ifdef AZTEC_36
  52. FILE *FDECL (freopen, (char *, char *, FILE *));
  53. #endif
  54.  
  55. int
  56. main(argc, argv)
  57. int argc;
  58. char **argv;
  59. {
  60.     char    infile[64], outfile[64];
  61.     FILE    *fin, *fout;
  62.     int    i, len;
  63. #ifdef MAC_THINKC5
  64.     static char *mac_argv[] = {    "dgn_comp",    /* dummy argv[0] */
  65.                 ":dat:dungeon.pdf"
  66.                 };
  67.  
  68.     argc = SIZE(mac_argv);
  69.     argv = mac_argv;
  70. #endif
  71.  
  72.     if (argc == 1) {    /* Read standard input */
  73.         init_yyin(stdin);
  74.         init_yyout(stdout);
  75.         yyparse();
  76.     } else            /* Otherwise every argument is a filename */
  77.         for(i=1; i<argc; i++) {
  78.             fname = strcpy(infile, argv[i]);
  79.             len = strlen(fname) - 4;    /* length excluding suffix */
  80.             if (len < 0 || strncmp(".pdf", fname + len, 4)) {
  81.             fprintf(stderr,
  82.                 "Error - file name \"%s\" in wrong format.\n",
  83.                 fname);
  84.             continue;
  85.             }
  86. #ifdef VMS    /* make sure to avoid possible interaction with logical name */
  87.             len++;    /* retain "." as trailing punctuation */
  88. #endif
  89.             (void) strncpy(outfile, infile, len);
  90.             outfile[len] = '\0';
  91.  
  92.             fin = freopen(infile, "r", stdin);
  93.             if (!fin) {
  94.             fprintf(stderr,"Can't open %s for input\n", infile);
  95.             perror(infile);
  96.             continue;
  97.             }
  98.             fout = freopen(outfile, WRMODE, stdout);
  99.             if (!fout) {
  100.             fprintf(stderr,"Can't open %s for output\n", outfile);
  101.             perror(outfile);
  102.             continue;
  103.             }
  104.             init_yyin(fin);
  105.             init_yyout(fout);
  106.             yyparse();
  107.             line_number = 1;
  108.             fatal_error = 0;
  109.         }
  110. #ifndef VMS
  111.     return 0;
  112. #else
  113.     return 1;       /* vms success */
  114. #endif /*VMS*/
  115. }
  116.  
  117. /*
  118.  * Each time the parser detects an error, it uses this function.
  119.  * Here we take count of the errors. To continue farther than
  120.  * MAX_ERRORS wouldn't be reasonable.
  121.  */
  122.  
  123. void yyerror(s)
  124. char *s;
  125. {
  126.     fprintf(stderr,"%s : line %d : %s\n",fname,line_number, s);
  127.     if (++fatal_error > MAX_ERRORS) {
  128.         fprintf(stderr,"Too many errors, good bye!\n");
  129.         exit(1);
  130.     }
  131. }
  132.  
  133. /*
  134.  * Just display a warning (that is : a non fatal error)
  135.  */
  136.  
  137. void yywarning(s)
  138. char *s;
  139. {
  140.     fprintf(stderr,"%s : line %d : WARNING : %s\n",fname,line_number,s);
  141. }
  142.  
  143. int yywrap()
  144. {
  145.     SpinCursor(3); /*    Don't know if this is a good place to put it ?
  146.                         Is it called for our grammar ? Often enough ?
  147.                         Too often ? -- h+ */
  148.        return 1;
  149. }
  150.  
  151. /*dgn_main.c*/
  152.